import { constantRouterMap } from '@/router'
// import { asyncRouterMap } from '@/router'
import { getCurrentPermissions } from '@/api/system/permission'
/* Layout */
import Layout from '@/views/layout/Layout'

const _import = require('@/router/_import_' + process.env.NODE_ENV)

/**
 * 通过meta.role判断是否与当前用户权限匹配
 * @param roles
 * @param route
 */
// function hasPermission(roles, route) {
//   if (route.meta && route.meta.roles) {
//     return roles.some(role => route.meta.roles.indexOf(role) >= 0)
//   } else {
//     return true
//   }
// }

/**
 * 递归过滤异步路由表，返回符合用户角色权限的路由表
 * @param asyncRouterMap
 * @param roles
 */
// function filterAsyncRouter(asyncRouterMap, roles) {
//   const accessedRouters = asyncRouterMap.filter(route => {
//     if (hasPermission(roles, route)) {
//       if (route.children && route.children.length) {
//         route.children = filterAsyncRouter(route.children, roles)
//       }
//       return true
//     }
//     return false
//   })
//   return accessedRouters
// }

const permission = {
  state: {
    routers: constantRouterMap,
    addRouters: []
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers
      state.routers = constantRouterMap.concat(routers)
    }
  },
  actions: {
    GenerateRoutes({ commit }, data) {
      // return new Promise(resolve => {
      //   const { roles } = data
      //   let accessedRouters
      //   if (roles.indexOf('admin') >= 0) {
      //     accessedRouters = asyncRouterMap
      //   } else {
      //     accessedRouters = filterAsyncRouter(asyncRouterMap, roles)
      //   }
      //   commit('SET_ROUTERS', accessedRouters)
      //   resolve()
      // })
      return new Promise(resolve => {
        // 改为从数据库获取权限菜单
        getCurrentPermissions().then(response => {
          // const accessedRouters = asyncRouterMap
          const accessedRouters = formatRoutes(response.data)
          // 最后添加404错误
          const unfound = { path: '*', redirect: '/404', hidden: true }
          accessedRouters.push(unfound)
          commit('SET_ROUTERS', accessedRouters)
          resolve()
        })
      })
    }
  }
}

export default permission

// 格式化路由
export const formatRoutes = (oRoutes) => {
  const newRouters = []
  oRoutes.forEach(oRoute => {
    const {
      path,
      component,
      name,
      meta,
      children,
      hidden
    } = oRoute
    if (component) {
      const newRouter = {
        path: path,
        component: formatComponent(component),
        name: name,
        meta: meta,
        redirect: getRedirect(path, children),
        children: (!children) ? null : formatRoutes(children),
        hidden: hidden
      }
      newRouters.push(newRouter)
    }
  })
  return newRouters
}

// 格式化组件
export const formatComponent = (component) => {
  if (component === 'Layout' || component === 'layout') {
    return Layout
  } else {
    return _import(component)
  }
}

// 跳转链接
export const getRedirect = (path, children) => {
  if (!children) {
    return null
  } else {
    // 默认跳转第一个子菜单链接
    return path + '/' + children[0].path
  }
}
